package controller.util;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import controller.action.Action;


public class ActionMapper {
	private static HashMap<String, Action> actionList= new HashMap<String, Action>();
	
	public static void mappingAction(String path){
		Properties proAction =new Properties();
		try {
			proAction.load(new FileInputStream(path));
		} catch (Exception e) {
			System.out.println("fail to load a file");
			e.printStackTrace();
		}
		Set<Object> keySet= proAction.keySet();
		Iterator<Object> keyIterator= keySet.iterator();
		String key = null;
		Action action=null;

		while(keyIterator.hasNext()){
			key=(String)keyIterator.next();
			try {
				action =(Action)Class.forName(proAction.getProperty(key)).newInstance();
			} catch (Exception e) {
				System.out.println("fail to make a "+ proAction.getProperty(key)+ "Instance");
				e.printStackTrace();
			}
			actionList.put(key, action);
		}
	}
	
	public static Action getAction(String command){
		Action action=null;
		action = actionList.get(command);
		return action;
	}
}
